package com.ruoyi.web.controller.erp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SalesOrder;
import com.ruoyi.system.domain.SalesOrderItems;
import com.ruoyi.system.service.ISalesOrderService;
import com.ruoyi.system.service.IProgressTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 销售单Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/erp/salesOrder")
public class SalesOrderController extends BaseController
{
    @Autowired
    private ISalesOrderService salesOrderService;

    @Autowired
    private IProgressTemplateService progressTemplateService;

    /**
     * 查询销售单列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SalesOrder salesOrder)
    {
        startPage();
        List<SalesOrder> list = salesOrderService.selectSalesOrderList(salesOrder);
        return getDataTable(list);
    }

    /**
     * 获取销售单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:query')")
    @GetMapping(value = "/{salesOrderId}")
    public AjaxResult getInfo(@PathVariable("salesOrderId") Long salesOrderId)
    {
        return success(salesOrderService.selectSalesOrderById(salesOrderId));
    }

    /**
     * 新增销售单
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:add')")
    @Log(title = "销售单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalesOrder salesOrder)
    {
        salesOrder.setCreatedBy(SecurityUtils.getUserId());
        return toAjax(salesOrderService.insertSalesOrder(salesOrder));
    }

    /**
     * 新增销售单明细
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:add')")
    @Log(title = "销售单管理", businessType = BusinessType.INSERT)
    @PostMapping("/items")
    public AjaxResult addItems(@RequestBody SalesOrderItems item)
    {
        return toAjax(salesOrderService.insertSalesOrderItems(item));
    }

    /**
     * 批量新增销售单明细
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:add')")
    @Log(title = "销售单管理", businessType = BusinessType.INSERT)
    @PostMapping("/batchItems")
    public AjaxResult batchAddItems(@RequestBody List<SalesOrderItems> items)
    {
        return toAjax(salesOrderService.batchInsertSalesOrderItems(items));
    }

    /**
     * 从报价单创建销售单
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:add')")
    @Log(title = "销售单管理", businessType = BusinessType.INSERT)
    @PostMapping("/createFromQuotation/{quotationId}")
    public AjaxResult createFromQuotation(@PathVariable Long quotationId)
    {
        return toAjax(salesOrderService.createFromQuotation(quotationId));
    }

    /**
     * 修改销售单
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:edit')")
    @Log(title = "销售单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalesOrder salesOrder)
    {
        return toAjax(salesOrderService.updateSalesOrder(salesOrder));
    }

    /**
     * 删除销售单
     */
//    @PreAuthorize("@ss.hasPermi('erp:salesOrder:remove')")
    @Log(title = "销售单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{salesOrderIds}")
    public AjaxResult remove(@PathVariable Long[] salesOrderIds)
    {
        return toAjax(salesOrderService.deleteSalesOrderByIds(salesOrderIds));
    }

    /**
     * 获取销售单进度
     */
    @GetMapping("/progress/{salesOrderId}")
    public AjaxResult getProgress(@PathVariable Long salesOrderId) {
        Map<String, Object> progress = progressTemplateService.getOrderProgress(salesOrderId, "销售单");
        return AjaxResult.success(progress);
    }

    /**
     * 更新销售单状态
     */
    @PutMapping("/status/{salesOrderId}")
    public AjaxResult updateStatus(
        @PathVariable Long salesOrderId,
        @RequestParam String status) 
    {
        SalesOrder salesOrder = new SalesOrder();
        salesOrder.setSalesOrderId(salesOrderId);
        salesOrder.setStatus(status);
        return toAjax(salesOrderService.updateSalesOrder(salesOrder));
    }
}
